package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Constants;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.NcgDeviceWatch;
import com.sqx.modules.health.entity.Receive;
import com.sqx.modules.health.service.INcgDeviceWatchService;
import com.sqx.modules.health.service.IReceiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gxy
 * @since 2022-05-13
 */

@Api(value = "", tags = "", description = "")
@RestController
@RequestMapping("/health/receive")
public class ReceiveController {
    @Autowired
    private IReceiveService receiveService;

    @Autowired
    private INcgDeviceWatchService deviceWatchService;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新", notes = "新增或更新")
    public Result save(@RequestBody Receive receive) {
        return Result.success(receiveService.saveOrUpdate(receive));
    }

    // 根据ID删除
    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    public Result delete(@PathVariable Integer id) {
        return Result.success(receiveService.removeById(id));
    }

    // 根据IDS删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除", notes = "根据IDS删除")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(receiveService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部", notes = "查询全部")
    public Result findAll() {
        return Result.success(receiveService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录", notes = "根据ID查询一条记录")
    public Result findOne(@RequestParam Integer id) {
        return Result.success(receiveService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false, defaultValue = "") Long uid,
                           @RequestParam(required = false, defaultValue = "") String type) {
        QueryWrapper<Receive> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isEmpty(uid)) {
            return Result.error(Constants.CODE_500, "参数错误!");
        }
        // 根据 uid 定位设备
        QueryWrapper<NcgDeviceWatch> deviceWrapper = new QueryWrapper<>();
        deviceWrapper.eq("uid", uid);
        deviceWrapper.eq("type", 3);
        NcgDeviceWatch deviceWatch = deviceWatchService.getOne(deviceWrapper);
        if (ObjectUtil.isEmpty(deviceWatch)) {
            return Result.error(Constants.CODE_500, "用户未绑定设备!!!");
        }
        // 获取定位到设备的 设备编码
        String deviceNo = deviceWatch.getImei();

        // 根据设备编码获取检查数据
        queryWrapper.eq("devSN", deviceNo);
        queryWrapper.eq(ObjectUtil.isNotEmpty(type), "deviceType", type);
        queryWrapper.orderByDesc("createtime");
        return Result.success(receiveService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


}

